import 'package:flutter/material.dart';
import 'package:flutter_bmfmap_example/models/goodmodel.dart';
import 'package:flutter_bmfmap_example/service/marketservice.dart';
import 'package:flutter_easyrefresh/easy_refresh.dart';
import 'goods.dart';

class GoodsListview extends StatefulWidget {
  GoodsListview({Key key,this.type}) : super(key: key);

  String type;

  @override
  _GoodsListviewState createState() => _GoodsListviewState();
}

class _GoodsListviewState extends State<GoodsListview> with AutomaticKeepAliveClientMixin{
  List<GoodItem> _goodslist = GoodsList([]).list;
  int page = 1;
  bool hasmore = true;

  EasyRefreshController _easyRefreshcontroller;

  @override
  void initState() {
    print('init');
    // TODO: implement initState
    super.initState();
    _easyRefreshcontroller = EasyRefreshController();
    // _getgoodslist();
  }

  _getgoodslist({bool push = false}) async {
    var result;
    if(widget.type=='全部')result = await Marketservice.getgoodslist(page);
    else result = await Marketservice.getgoodslistbytype(widget.type, page);
    GoodsList goodsList = GoodsList.fromJson(result['list']);
    
    setState(() {
      hasmore = result['hasNextPage'];
      page++;
      if(push){
        _goodslist.addAll(goodsList.list);
      }
      else{
        _goodslist = goodsList.list;
      }
    });
  }

  Future _onRefresh() async {
    page = 1;
    await _getgoodslist(push: false);
    _easyRefreshcontroller.finishRefresh(noMore:false);
    _easyRefreshcontroller.resetLoadState();
  }

  Future _onLoad() async {
    print('hasmore'+hasmore.toString());
    if(hasmore){
     await  _getgoodslist(push: true);
    }
    _easyRefreshcontroller.finishLoad(noMore: !hasmore);
  }

  @override
  bool get wantKeepAlive =>true;

  @override
  Widget build(BuildContext context) {
    super.build(context);
    return Padding(
      padding: const EdgeInsets.only(left: 16,right: 16),
      child: EasyRefresh(
        // firstRefresh: true,
      // 加载时的loading
      // firstRefreshWidget: PageFeedBack(firstRefresh: true).build(),
        controller: _easyRefreshcontroller,
        onLoad: _onLoad,
        onRefresh: _onRefresh,
        footer: ClassicalFooter(),
        header: ClassicalHeader(),
        child: GridView.builder(
            // physics: BouncingScrollPhysics(),
            itemCount: _goodslist.length,
            //SliverGridDelegateWithFixedCrossAxisCount 构建一个横轴固定数量Widget
            gridDelegate: SliverGridDelegateWithFixedCrossAxisCount(
                //横轴元素个数
                crossAxisCount: 2,
                //纵轴间距
                mainAxisSpacing: 16.0,
                //横轴间距
                // crossAxisSpacing: 16.0,
                //子组件宽高长度比例
                childAspectRatio: 0.8),
            itemBuilder: (BuildContext context, int index) {
              // print(index);
              //Widget Function(BuildContext context, int index)
              return GoodsCard(
                name: _goodslist[index].name,
                price: _goodslist[index].money,
                imgurl1: _goodslist[index].imgurl1,
                id: _goodslist[index].commodityId,
                tagname: widget.type+index.toString(),
              );
            }),
      ),
    );
  }
}
